﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    ingredientDAO dao;
    Ingredient ingredient;
    protected void Page_Load(object sender, EventArgs e)
    {
        dao = new ingredientDAO();
        ingredient = new Ingredient();
        if (!IsPostBack)
        {
            load_cbbAnimal();
            load_cbbCategory();
        }
    }
    public void load_cbbAnimal()
    {
        DataSet ds = new DataSet();
        ds = dao.getcbbAnimal();
        cbbAnimal.DataSource = ds.Tables[0];
        cbbAnimal.DataTextField = "Animal";
        cbbAnimal.DataValueField = "AnimalID";
        cbbAnimal.DataBind();
    }
    public void load_cbbCategory()
    {
        DataSet ds = new DataSet();
        ds = dao.getcbbCategory();
        cbbCategory.DataSource = ds.Tables[0];
        cbbCategory.DataTextField = "Category";
        cbbCategory.DataValueField = "CategoryID";
        cbbCategory.DataBind();
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        insert();
    }
    public void insert()
    {
        DataSet ds = new DataSet();
        ds = (DataSet)Session["USER"];
        String userID = ds.Tables[0].Rows[0]["UserID"].ToString();
        ingredient.IngredientID1 = System.Guid.NewGuid().ToString();
        ingredient.Name1 = txtName.Text;
        ingredient.Price1 = Double.Parse(txtPrice.Text);
        ingredient.Dry1 = float.Parse(txtDry.Text);
        ingredient.CategoryID1 = cbbCategory.SelectedValue;
        ingredient.Code1 = txtCode.Text;
        ingredient.AnimalID1 = cbbAnimal.SelectedValue;
        if (chkInStock.Checked)
        {
            ingredient.InStock1 = true;
        }
        else ingredient.InStock1 = false;

        dao.addNew(ingredient, userID);

        Session.Add("IngredientID", ingredient.IngredientID1);
        Response.Redirect("addNutrientToIngredient.aspx");
    }
    protected void reset_Click(object sender, EventArgs e)
    {
        Response.Redirect("addNutrientToIngredient.aspx");
        
        
    }
}